package com.clz.hyperit.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.Position;
import com.clz.hyperit.entity.Question;
import com.clz.hyperit.entity.Tag;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.service.PositionService;
import com.clz.hyperit.service.TagService;
import com.clz.hyperit.service.UserDetailService;
import com.clz.hyperit.util.ResultMessage;
import com.clz.hyperit.vo.PageInfo;
import com.clz.hyperit.vo.Question_User;
import com.clz.hyperit.vo.UserArticle;
@RestController
@RequestMapping(value = "/tag")
public class TagController {
	private ResultMessage result = null;
	private final Logger logger = LoggerFactory.getLogger(PositionController.class);
	@Autowired
	private TagService tagService;
	
	@Autowired
	private UserDetailService userDetailService;
	
	@RequestMapping(value = "/saveTag", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> savePosition(Tag tag){
		result = new ResultMessage();
		if(tagService.getByName(tag.getName())!=null){
			result.setResultInfo("已存在该标签");
			result.setServiceResult(false);
		}
		else{
			tag.setArticle(0);
			tag.setQuestion(0);
			tagService.saveTag(tag);
			result.setResultInfo("创建标签成功");
			result.setServiceResult(true);
		}
		Tag tag_vo = tagService.getByName(tag.getName());
		result.getResultParm().put("tag",tag_vo);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/deleteTag", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deletePosition(Tag tag){
		result = new ResultMessage();
		tagService.deleteTag(tag);
		result.setResultInfo("删除标签成功");
		result.setServiceResult(true);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	@RequestMapping(value = "/updateTag", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> updatePosition(Tag tag){
		result = new ResultMessage();
		tagService.updateTag(tag);
		result.setResultInfo("更新标签成功");
		result.setServiceResult(true);
		result.getResultParm().put("tag_id",tag.getId());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	@RequestMapping(value = "/findTag", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findTag(){
		result = new ResultMessage();
		List<Tag> taglist = tagService.findTag();
		result.setResultInfo("标签获取成功");
		result.setServiceResult(true);
		result.getResultParm().put("taglist",taglist);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 获取指定tag
	 * author DIkuni
	 * @return
	 */
	@RequestMapping(value = "/findTheTag", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findTheTag(Integer id){
		result = new ResultMessage();
//		List<Tag> taglist = tagService.findTag();
		Tag tag = tagService.findById(id);
		result.setResultInfo("标签获取成功");
		result.setServiceResult(true);
		result.getResultParm().put("tag",tag);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 关联文章标签
	 * @return
	 */
	@RequestMapping(value = "/saveTagArticle", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> saveTagArticle(Integer article_id,String name){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Integer> list = tagService.saveTagArticle(article_id,name);
		if(list.size()<=0){
			result.setResultInfo("有错误标签");
			result.setServiceResult(false);
		}
		else{
			result.getResultParm().put("id_list",list);
			result.setServiceResult(true);
		}
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 根据标签获取文章列表
	 */
	@RequestMapping(value = "/getArticle", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getArticle(Integer tag_id,PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Article> article_list = tagService.getArticle(tag_id,pageInfo);
		List<UserArticle> user_article = getUserArticle_list(article_list);
		result.setResultInfo("根据标签获取文章成功");
		result.getResultParm().put("user_article",user_article);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 根据标签获取问题列表
	 */
	@RequestMapping(value = "/getQuestion", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getQuestion(Integer tag_id,PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Question> question_list = tagService.getQuestion(tag_id,pageInfo);
		List<Question_User> user_question = getUser_question(question_list);
		result.setResultInfo("根据标签获取问题成功");
		result.getResultParm().put("user_question",user_question);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 排序->按照问题+文章数量
	 * @return
	 */
	@RequestMapping(value = "/findTagByReading", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findTagByReading(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Tag> taglist = tagService.findTagByReading(pageInfo);
		result.setResultInfo("标签获取成功");
		result.setServiceResult(true);
		result.getResultParm().put("taglist",taglist);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	
	/**
	 * 排序->按照时间
	 * @return
	 */
	@RequestMapping(value = "/findTagByTime", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findTagByTime(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Tag> taglist = tagService.findTagByTime(pageInfo);
		result.setResultInfo("标签获取成功");
		result.setServiceResult(true);
		result.getResultParm().put("taglist",taglist);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 排序->按照文章数量
	 * @return
	 */
	@RequestMapping(value = "/findTagByArticleNum", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findTagByArticleNum(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Tag> taglist = tagService.findTagByArticleNum(pageInfo);
		result.setResultInfo("标签获取成功");
		result.setServiceResult(true);
		result.getResultParm().put("taglist",taglist);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 排序->按照问题数量
	 * @return
	 */
	@RequestMapping(value = "/findTagByQuestionNum", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findTagByQuestionNum(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Tag> taglist = tagService.findTagByQuestionNum(pageInfo);
		result.setResultInfo("标签获取成功");
		result.setServiceResult(true);
		result.getResultParm().put("taglist",taglist);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 搜索标签
	 * @param pageInfo
	 * @param tag
	 * @return
	 */
	@RequestMapping(value = "/searchTag", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> searchTag(PageInfo pageInfo,String tag){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Tag> taglist = tagService.findTagBySearch(pageInfo,tag);
		result.getResultParm().put("taglist",taglist);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	private List<UserArticle> getUserArticle_list(List<Article> article_list){
		List<UserArticle> list = new ArrayList<UserArticle>();
		for(Article article:article_list){
			UserDetail ud = userDetailService.getUserInfo(article.getUserDetail_id());
			article.setContent_md(null);
			article.setContent_html(null);
			list.add(new UserArticle(article,ud,tagService.findTagByArticle(article.getId())));
		}
		return list;
	}
	
	private Question_User setQustion_User(UserDetail ud,Question question,List<Tag> tag){
		Question_User qu = new Question_User(ud.getName(),ud.getHead(),
				question.getId(), question.getTitle(),
				question.getContent(), question.getUserDetail_id(),
				question.getCreatetime(), question.getAccept(),
				question.getAnswer(), question.getReading(),tag);
		return qu;
	}
	
	private List<Question_User> getUser_question(List<Question> question_list){
		List<Question_User> user_question_list = new ArrayList<Question_User>();
		for(Question question:question_list){
			UserDetail ud = userDetailService.getUserInfo(question.getUserDetail_id());
			question.setContent(null);
			Question_User qu = setQustion_User(ud,question,	tagService.findTagByQuestion(question.getId()));
			user_question_list.add(qu);
		}
			return user_question_list;
	}
}
